package com.tyx.chargingpile.service;

import com.tyx.chargingpile.cache.SocketChannelMap;
import com.tyx.chargingpile.pojo.HeartInfo;
import com.tyx.chargingpile.util.Util;
import com.tyx.chargingpile.vo.ChargingPileInfo;
import com.tyx.chargingpile.vo.GunInfo;
import com.tyx.chargingpile.vo.PileInfo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;

/**
 * 业务接口类
 */
@Service
public class OutPutService {

    /**
     * 获取缓存的电桩信息
     * @param pileId
     * @return
     */
    public ChargingPileInfo getChargingPileInfo(String pileId){
        PileInfo pileInfo = SocketChannelMap.getPileInfo(pileId);
        ChargingPileInfo chargingPileInfo = new ChargingPileInfo();
        if(pileInfo == null){
            chargingPileInfo.setDeviceId(pileId);
            chargingPileInfo.setDeviceStatus(0);
            chargingPileInfo.setResult(0);
            return chargingPileInfo;
        }
        Map<String, HeartInfo> heartInfo = pileInfo.getHeartInfoMap();
        if(heartInfo.size() != 0){
            chargingPileInfo.setGunList(new ArrayList<>());
        }
        for(Map.Entry<String, HeartInfo> entry:heartInfo.entrySet()){
            HeartInfo info = entry.getValue();
            GunInfo gunInfo = new GunInfo();
            byte[] batteryTemp = Util.String2BCD(info.getBatteryTemp());
            gunInfo.setBatteryTemperature(Util.HexToint(batteryTemp,batteryTemp.length));
            byte[] batteryType = Util.String2BCD(info.getBatteryType());
            gunInfo.setBatteryType(Util.HexToint(batteryType,batteryType.length));
            byte[] chargingTime = Util.String2BCD(info.getChargingTime());
            gunInfo.setChargeTime(Util.HexToint(chargingTime,chargingTime.length));
            byte[] PileTemp = Util.String2BCD(info.getPileTemp());
            gunInfo.setDeviceTemperature(Util.HexToint(PileTemp,PileTemp.length));
            byte[] faultCode = Util.String2BCD(info.getWarinCode());
            gunInfo.setFaultCode(Util.HexToint(faultCode,faultCode.length));
//            gunInfo.setGunId(info.getGunpointNum());
            gunInfo.setGunId(Integer.parseInt(info.getGunpointNum()) + "");
            byte[] statusHex = Util.String2BCD(info.getStatus());
            int status = Util.HexToint(statusHex,statusHex.length);
            gunInfo.setGunStatus(status);
            byte[] gunTemp = Util.String2BCD(info.getGunTemp());
            gunInfo.setGunTemperature(Util.HexToint(gunTemp,gunTemp.length));
            gunInfo.setOrderId(info.getOrderNum());
            gunInfo.setRatedAmpere(Double.valueOf(info.getaElectricCurrent())/100);
            gunInfo.setRatedAmpereB(Double.valueOf(info.getbElectricCurrent())/100);
            gunInfo.setRatedAmpereC(Double.valueOf(info.getcElectricCurrent())/100);
            gunInfo.setRatedVoltage(Double.valueOf(info.getaVoltage())/100);
            gunInfo.setRatedVoltageB(Double.valueOf(info.getbVoltage())/100);
            gunInfo.setRatedVoltageC(Double.valueOf(info.getcVoltage())/100);
            gunInfo.setRatedMoney(Double.valueOf(info.getConsumeMoney())/100);
            byte[] soc = Util.String2BCD(info.getSoc());
            gunInfo.setRatedSOC(Util.HexToint(soc,soc.length));
            gunInfo.setRatedElectric(Double.valueOf(info.getConsumeElectric())/100);
            byte[] surplusTime = Util.String2BCD(info.getSurplusTime());
            gunInfo.setRestTime(Util.HexToint(surplusTime,surplusTime.length));
            gunInfo.setUnitHeighV(Integer.valueOf(info.getHighVoltage()));
            gunInfo.setUnitLowestV(Integer.valueOf(info.getMiniVoltage()));
            chargingPileInfo.addGunList(gunInfo);
        }
        if (chargingPileInfo.getGunList() != null){
            chargingPileInfo.setGunNum(chargingPileInfo.getGunList().size());
        }
        chargingPileInfo.setDeviceId(pileId);
        chargingPileInfo.setDeviceStatus(1);
        chargingPileInfo.setResult(1);
        return chargingPileInfo;
    }
}
